﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Proje
{
    class BusFileConnector
    {
      
        FileStream file;
        StreamReader reader;
        StreamWriter writer;

        public List<string> selectAllBusPlaka(){
            List<string> plakas=new List<string>();
            file = new FileStream(FilePath, FileMode.Open);
            reader = new StreamReader(file);
            while (reader.ReadLine() != null) {
                string bus = reader.ReadLine();
                if (!bus.Split('|')[5].Equals("1"))
                    plakas.Add(bus.Split('|')[0]);
                }
            return plakas;
        
        }
        public bool insert(Bus bus){
            try
            {
                file = new FileStream(FilePath, FileMode.Append);
                writer = new StreamWriter(file);
                string recordString = createInsertString(bus);
                writer.WriteLine(recordString);
                return true;
            }
            catch (IOException e)
            {
                return false;
            }
            finally {
                writer.Close();
                file.Close();
            }
        }
        private string createInsertString(Bus bus) {
            return string.Format(
                    "{0}|{1}|{2}|{3}|{4}|{5}",
                    bus.plaka,
                    bus.brand,
                    bus.model,
                    bus.year,
                    bus.seats,
                    bus.status
                );
        }
        private string FilePath {
            get { 
                return @"C:\Users\AHMETCAN\Desktop\otobus.txt";
            }
        }
    }

 
}
